
var base  = require("./base.js");
var ws  = require("./ws.js");

// #ifdef APP-PLUS || MP-WEIXIN || MP-QQ || MP-BAIDU || MP-ALIPAY
// #endif

module.exports = {
    g: {
        appname: '协同管理系统',
        copyright: '厦门二次方网络科技有限公司',
		
		adminid: 0,
		admininfo: [],
		user_openid:'',
		user_session_key:'',
		ws: false,
		ws_timer: false,
    },
	
	debug_set: function(){
		if(base.config.env == 'local')
		{
			console.log('debug_set start')
			var that = this;
			uni.setStorageSync('adminid', that.g.adminid)
			uni.setStorageSync('admininfo', JSON.stringify(that.g.admininfo))
			console.log('debug_set end')			
		}
	},
	debug_get: function(){
		if(base.config.env == 'local')
		{
			console.log('debug_get start')
			var that = this;
			that.g.adminid = uni.getStorageSync('adminid')
			var admininfo = uni.getStorageSync('admininfo')
			if(admininfo) that.g.admininfo = JSON.parse(admininfo)
			console.log('debug_get end')			
		}
	},
	
    //程序更新
    update: function (remind) {
        // const updateManager = uni.getUpdateManager();
        // updateManager.onCheckForUpdate(function (res) {
        //     if (res.hasUpdate) {
        //         uni.showLoading({ title: '正在更新', mask: true })
        //         updateManager.onUpdateReady(function () {
        //             updateManager.applyUpdate()
        //         })
        //     }
        // });
		
		if(!remind) remind = 0;
		var that = this;		
		//#ifdef APP-PLUS  	
		base.request({
			url: base.url('pda_common/update'),
			data: {
				"appid": plus.runtime.appid,  
				// "version": plus.runtime.version,
				"version": base.config.version
			},
			success: function(res) {
				uni.hideLoading()
				if (res.data.success > 0 && res.data.status == 1) {
					uni.showModal({  
						title: "更新提示",  
						content: res.data.note,  
						// showCancel: false,
						success: (re) => {  							
							if(!remind) {
								plus.os.name == "Android" ? plus.runtime.quit() : plus.ios.import("UIApplication").sharedApplication().performSelector("exit"); //强制关闭APP
							}
							if (re.confirm) {  
								plus.runtime.openURL(res.data.url);  
							} 
						}  
					})  			
				}
				else if(remind) {
					base.ui.toast('已是最新版本') 
				}
			},
			fail: function (res){
			}
		});		
		//#endif  	
    },
	initUser: function (login_func, update_func) {
		var that = this;
		uni.login({
			provider: that.g.provider,
			timeout: 10 * 1000,
			success:res =>{
				if (res.code) {
				    base.request({
						// #ifdef MP-WEIXIN
				        url: base.urlweixin('weixin_min/session') ,
						// #endif							
				        data: { code: res.code },
				        success: function (res) {
							
				            if (res.data.success > 0) {
								that.g.user_openid = res.data.openid;
								that.g.session_key = res.data.session_key;
				                if (typeof login_func == 'function') login_func();
				            }
				            else {
				                uni.showToast({
				                    title: '登录失败！', icon: 'none', duration: 3000
				                });
				            }
				        }
				    });
				}else{
					uni.showToast({
					    title: '无法获取授权', icon: 'none', duration: 3000
					});
				}
			}
			
		})
	},
	ws_start: function(func){
		var that = this;
		if (parseInt(that.g.adminid) > 0) {
			clearTimeout(that.ws_timer);
			ws.ws.add_func('alert', func)
			ws.ws.start({ 
				cmd: 'reg', 
				group: 'ps-pda-' + that.g.adminid,
				wsid: 'wsid-' + that.g.adminid + '-' + Math.random(),
			}, true)
		}
		else {
			that.ws_timer = setTimeout(function () {
				that.ws_start();
			}, 1000);
		}		
	},
	
	ws_close: function(){
		clearTimeout(that.ws_timer);
		ws.ws.close()
	}
}